$(function () {
    $("#jqGrid").Grid({
        url: '../factorytype/list?type=2',
        rownumWidth: 40,
        colModel: [
            {label: '厂家名称', name: 'faName', index: 'FA_NAME', width: 50},
            {label: '厂家编码', name: 'faCode', index: 'FA_CODE', width: 30},
            {label: '厂家标识', name: 'selfCode', index: 'SELF_CODE', width: 30},
            {label: '备注', name: 'remark', index: 'REMARK'},
            {name: 'type', index: 'TYPE', hidden: true},
            {name: 'id', key: true, hidden: true}
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        factoryType: {isDel: 1, remark: '', faName: '', faCode: '', id: '',selfCode:''},
        ruleValidate: {
            faName: [
                {required: true, message: '厂家名称必填', trigger: 'blur'}
            ],
            selfCode: [
                {required: true, message: '厂家标识必填', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            code:'',
            selfCode:'',
            remark: '' // 厂家备注
        }
    },
    methods: {
        query: function () {
            vm.reload(1);
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.factoryType = {remark: '', faName: '', id: '',type: 2,selfCode:''};
            $("#doSave").attr("disabled", false);
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            $("#doSave").attr("disabled", false);
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            $("#doSave").attr("disabled", true);
            let url = vm.factoryType.id == (null || '') ? "../factorytype/save" : "../factorytype/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.factoryType),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../factorytype/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../factorytype/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.factoryType = r.factoryType;
                }
            });
        },
        reload: function (pageNum) {
            vm.showList = true;
            let page = pageNum?pageNum:$("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'code':vm.q.code,'type':vm.q.type,'remark':vm.q.remark,'selfCode': vm.q.selfCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                code:'',
                remark:'',
                selfCode:''
            };
            vm.reload(1);
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (!res.err) {
                this.$Loading.finish();
                alert('导入成功', function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.err
                })
            }
        },
        handleError: function (err, file) {
            alert('导入失败');
        },
    }
});
